package problem904;

//904.水果成篮
//https://leetcode.cn/problems/fruit-into-baskets/

import java.util.HashMap;

class Solution {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int ret = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0, j = 0; j<n; j++) {
            map.put(fruits[j], map.getOrDefault(fruits[j], 0) + 1);
            while(map.size() > 2) {
                map.put(fruits[i], map.get(fruits[i]) - 1);
                if(map.get(fruits[i]) == 0) map.remove(fruits[i]);
                i++;
            }
            ret = Math.max(ret, j-i+1);
        }
        return ret;
    }
}